<?php
namespace Xcx\Model;

use Think\Model;
class LoginModel extends Model
{
	protected $tableName = 'users';

	/**
	 * 检查用户是否存在
	 * @param string $openId
	 * @return boolean
	 */
	public function checkUserIsExist($openId)
	{
		$data = M('users')->where(array('openid'=>$openId))->find();
		
		if (is_array($data) && count($data) > 0) {
			return true;
		}
		return false;
	}

    /**
     * 将小程序登录信息存入数据库
     * @param $data
     * @return bool
     */
	public function saveLoginSession($data)
    {
        return M('login_session')->data($data)->add();
    }

    /**
     * @param $session3rd
     * @return array|false
     */
    public function getLoginSession($session3rd)
    {
        $maps['session3rd'] = $session3rd;
        return M('login_session')->where($maps)->order(array('login_time'=>'desc'))->find();
    }

    /**
     * 添加用户信息
     * @param $data
     * @return mixed
     */
	public function addUserInfo($data)
	{
		return M('users')->data($data)->add();
	}

    /**
     * 获取用户信息
     * @param $openId
     * @return array|false|mixed|\PDOStatement|string|Model
     */
	public function getUserInfo($openId)
    {
        $user = M('users')->where(array('openid'=>$openId))->find();
        if (is_array($user) && count($user) > 0) {
            $header = getUploadImgDir($user['header'],'user_head');
            $user['header'] = $header;
            return $user;
        }
        return array();
    }
}